﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using CF_DAL_WS.Models;
namespace CF_DAL_WS
{
	#region Using
	using CF_DAL_WS.Models;
	using CF_DAL_WS.Repositories;
	using CF_DAL_WS.Helpers;
	using CF_DAL_WS.DTO;
    using System.IO;
    using System.Web.Hosting;
	#endregion
	public class CF_DAL_Service: ICF_DAL_Service
	{
		/////////////////// FOODTYPE ///////////////////
		public List<FoodTypeDTO> FoodType_SelectAll()
		{
			var lstDto = new List<FoodTypeDTO>();
			var rpt = new FoodTypeRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(FoodTypeDTO.Translate(item));
			}
			return lstDto;
		}
		public FoodTypeDTO FoodType_SelectByID(string id)
		{
			var rpt = new FoodTypeRepository();
			return FoodTypeDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public bool FoodType_Insert(FoodTypeDTO obj)
		{
			var rpt = new FoodTypeRepository();
			var entity = FoodTypeDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool FoodType_Delete(string id)
		{
			var rpt = new FoodTypeRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool FoodType_Update(FoodTypeDTO obj)
		{
			var rpt = new FoodTypeRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<FoodTypeDTO> FoodType_RetrieveByID(string id)
		{
			var rpt = new FoodTypeRepository();
			var lstDto = new List<FoodTypeDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(FoodTypeDTO.Translate(item));
			}
			return lstDto;
		}
		/////////////////// MENU ///////////////////
		public List<MenuDTO> Menu_SelectAll()
		{
			var lstDto = new List<MenuDTO>();
			var rpt = new MenuRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(MenuDTO.Translate(item));
			}
			return lstDto;
		}
		public MenuDTO Menu_SelectByID(string id)
		{
			var rpt = new MenuRepository();
            return MenuDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public bool Menu_Insert(MenuDTO obj)
		{
			var rpt = new MenuRepository();
			var entity = MenuDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool Menu_Delete(string id)
		{
			var rpt = new MenuRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool Menu_Update(MenuDTO obj)
		{
			var rpt = new MenuRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<MenuDTO> Menu_RetrieveByID(string id)
		{
			var rpt = new MenuRepository();
			var lstDto = new List<MenuDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(MenuDTO.Translate(item));
			}
			return lstDto;
		}
		public List<MenuDTO> Menu_SelectByIDRestaurant(string idrestaurant)
		{
			var rpt = new MenuRepository();
			var lstDto = new List<MenuDTO>();
            var lstEntity = rpt.SelectByIDRestaurant(int.Parse(idrestaurant));
			foreach (var item in lstEntity)
			{
				lstDto.Add(MenuDTO.Translate(item));
			}
			return lstDto;
		}
		/////////////////// INFORMATIONWEB ///////////////////
		public List<InformationWebDTO> InformationWeb_SelectAll()
		{
			var lstDto = new List<InformationWebDTO>();
			var rpt = new InformationWebRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(InformationWebDTO.Translate(item));
			}
			return lstDto;
		}
		public InformationWebDTO InformationWeb_SelectByID(string id)
		{
			var rpt = new InformationWebRepository();
            return InformationWebDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public bool InformationWeb_Insert(InformationWebDTO obj)
		{
			var rpt = new InformationWebRepository();
			var entity = InformationWebDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool InformationWeb_Delete(string id)
		{
			var rpt = new InformationWebRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool InformationWeb_Update(InformationWebDTO obj)
		{
			var rpt = new InformationWebRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<InformationWebDTO> InformationWeb_RetrieveByID(string id)
		{
			var rpt = new InformationWebRepository();
			var lstDto = new List<InformationWebDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(InformationWebDTO.Translate(item));
			}
			return lstDto;
		}
		/////////////////// RESTAURANT ///////////////////
		public List<RestaurantDTO> Restaurant_SelectAll()
		{
			var lstDto = new List<RestaurantDTO>();
			var rpt = new RestaurantRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(RestaurantDTO.Translate(item));
			}
			return lstDto;
		}
		public RestaurantDTO Restaurant_SelectByID(string id)
		{
			var rpt = new RestaurantRepository();
            return RestaurantDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public bool Restaurant_Insert(ref RestaurantDTO obj)
		{
			var rpt = new RestaurantRepository();
			var entity = RestaurantDTO.Reverse(obj);
			var result = rpt.Insert(entity);
            if (result)
            {
                obj.ID = entity.ID;
            }
            return result;
		}
		public bool Restaurant_Delete(string id)
		{
			var rpt = new RestaurantRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool Restaurant_Update(RestaurantDTO obj)
		{
            try
            {
                var rpt = new RestaurantRepository();
                var entity = rpt.SelectByID(obj.ID);
                obj.UpdateEntity(ref entity);
                return rpt.Save();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return false;
		}
		public List<RestaurantDTO> Restaurant_RetrieveByID(string id)
		{
			var rpt = new RestaurantRepository();
			var lstDto = new List<RestaurantDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(RestaurantDTO.Translate(item));
			}
			return lstDto;
		}
		public List<RestaurantDTO> Restaurant_SelectByIDDistrict(string iddistrict)
		{
			var rpt = new RestaurantRepository();
			var lstDto = new List<RestaurantDTO>();
            var lstEntity = rpt.SelectByIDDistrict(int.Parse(iddistrict));
			foreach (var item in lstEntity)
			{
				lstDto.Add(RestaurantDTO.Translate(item));
			}
			return lstDto;
		}
		public List<RestaurantDTO> Restaurant_SelectByIDFoodType(string idfoodtype)
		{
			var rpt = new RestaurantRepository();
			var lstDto = new List<RestaurantDTO>();
            var lstEntity = rpt.SelectByIDFoodType(int.Parse(idfoodtype));
			foreach (var item in lstEntity)
			{
				lstDto.Add(RestaurantDTO.Translate(item));
			}
			return lstDto;
		}
        public List<RestaurantDTO> Restaurant_SelectByIDDistrictAndIDFoodType(string iddistrict, string idfoodtype)
        {
            var rpt = new RestaurantRepository();
            var lstDto = new List<RestaurantDTO>();
            var lstEntity = rpt.SelectByIDDistrictAndIDFoodType(int.Parse(iddistrict), int.Parse(idfoodtype));
            foreach (var item in lstEntity)
            {
                lstDto.Add(RestaurantDTO.Translate(item));
            }
            return lstDto;
        }
		/////////////////// CONTACT ///////////////////
		public List<ContactDTO> Contact_SelectAll()
		{
			var lstDto = new List<ContactDTO>();
			var rpt = new ContactRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(ContactDTO.Translate(item));
			}
			return lstDto;
		}
		public ContactDTO Contact_SelectByID(string id)
		{
			var rpt = new ContactRepository();
            return ContactDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public bool Contact_Insert(ContactDTO obj)
		{
			var rpt = new ContactRepository();
			var entity = ContactDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool Contact_Delete(string id)
		{
			var rpt = new ContactRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool Contact_Update(ContactDTO obj)
		{
			var rpt = new ContactRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<ContactDTO> Contact_RetrieveByID(string id)
		{
			var rpt = new ContactRepository();
			var lstDto = new List<ContactDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(ContactDTO.Translate(item));
			}
			return lstDto;
		}
		/////////////////// REPLY ///////////////////
		public List<ReplyDTO> Reply_SelectAll()
		{
			var lstDto = new List<ReplyDTO>();
			var rpt = new ReplyRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(ReplyDTO.Translate(item));
			}
			return lstDto;
		}
		public ReplyDTO Reply_SelectByID(string id)
		{
			var rpt = new ReplyRepository();
            return ReplyDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public bool Reply_Insert(ReplyDTO obj)
		{
			var rpt = new ReplyRepository();
			var entity = ReplyDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool Reply_Delete(string id)
		{
			var rpt = new ReplyRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool Reply_Update(ReplyDTO obj)
		{
			var rpt = new ReplyRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<ReplyDTO> Reply_RetrieveByID(string id)
		{
			var rpt = new ReplyRepository();
			var lstDto = new List<ReplyDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(ReplyDTO.Translate(item));
			}
			return lstDto;
		}
		public List<ReplyDTO> Reply_SelectByIDContact(string idcontact)
		{
			var rpt = new ReplyRepository();
			var lstDto = new List<ReplyDTO>();
            var lstEntity = rpt.SelectByIDContact(int.Parse(idcontact));
			foreach (var item in lstEntity)
			{
				lstDto.Add(ReplyDTO.Translate(item));
			}
			return lstDto;
		}
		/////////////////// FOOD ///////////////////
		public List<FoodDTO> Food_SelectAll()
		{
			var lstDto = new List<FoodDTO>();
			var rpt = new FoodRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(FoodDTO.Translate(item));
			}
			return lstDto;
		}
		public FoodDTO Food_SelectByID(string id)
		{
			var rpt = new FoodRepository();
            return FoodDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public bool Food_Insert(ref FoodDTO obj)
		{
			var rpt = new FoodRepository();
			var entity = FoodDTO.Reverse(obj);
			var result = rpt.Insert(entity);
            if (result)
            {
                obj.ID = entity.ID;
            }
            return result;
		}
		public bool Food_Delete(string id)
		{
			var rpt = new FoodRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool Food_Update(FoodDTO obj)
		{
			var rpt = new FoodRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<FoodDTO> Food_RetrieveByID(string id)
		{
			var rpt = new FoodRepository();
			var lstDto = new List<FoodDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(FoodDTO.Translate(item));
			}
			return lstDto;
		}
		public List<FoodDTO> Food_SelectByIDMenu(string idmenu)
		{
			var rpt = new FoodRepository();
			var lstDto = new List<FoodDTO>();
            var lstEntity = rpt.SelectByIDMenu(int.Parse(idmenu));
			foreach (var item in lstEntity)
			{
				lstDto.Add(FoodDTO.Translate(item));
			}
			return lstDto;
		}
        /////////////////// FOOD DETAIL ///////////////////
        public List<FoodDetailDTO> FoodDetail_SelectAll()
        {
            var lstDto = new List<FoodDetailDTO>();
            var rpt = new FoodDetailRepository();
            var lstEntity = rpt.SelectAll();
            foreach (var item in lstEntity)
            {
                lstDto.Add(FoodDetailDTO.Translate(item));
            }
            return lstDto;
        }
        public FoodDetailDTO FoodDetail_SelectByID(string id)
        {
            var rpt = new FoodDetailRepository();
            return FoodDetailDTO.Translate(rpt.SelectByID(int.Parse(id)));
        }
        public bool FoodDetail_Insert(FoodDetailDTO obj)
        {
            var rpt = new FoodDetailRepository();
            var entity = FoodDetailDTO.Reverse(obj);
            return rpt.Insert(entity);
        }
        public bool FoodDetail_Delete(string id)
        {
            var rpt = new FoodDetailRepository();
            return rpt.Delete(int.Parse(id));
        }
        public bool FoodDetail_Update(FoodDetailDTO obj)
        {
            var rpt = new FoodDetailRepository();
            var entity = rpt.SelectByID(obj.ID);
            obj.UpdateEntity(ref entity);
            return rpt.Save();
        }
        public List<FoodDetailDTO> FoodDetail_RetrieveByID(string id)
        {
            var rpt = new FoodDetailRepository();
            var lstDto = new List<FoodDetailDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
            foreach (var item in lstEntity)
            {
                lstDto.Add(FoodDetailDTO.Translate(item));
            }
            return lstDto;
        }

		/////////////////// Users ///////////////////
		public List<UsersDTO> Users_SelectAll()
		{
			var lstDto = new List<UsersDTO>();
			var rpt = new UsersRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(UsersDTO.Translate(item));
			}
			return lstDto;
		}
		public UsersDTO Users_SelectByID(string id)
		{
			var rpt = new UsersRepository();
			return UsersDTO.Translate(rpt.SelectByID(Guid.Parse(id)));
		}
        public UsersDTO Users_SelectByUserName(string username)
        {
            var rpt = new UsersRepository();
            return UsersDTO.Translate(rpt.SelectByUserName(username));
        }
        public string Users_Insert(string username, string password, string email, string role, string name, string phone, string address)
		{
            var errorText = string.Empty;
			var rpt = new UsersRepository();
			return rpt.Insert(username, password, email, role, name, phone, address, ref errorText);
		}
		public bool Users_Delete(string id)
		{
			var rpt = new UsersRepository();
            return rpt.Delete(Guid.Parse(id));
		}
		public bool Users_Update(UsersDTO obj)
		{
			var rpt = new UsersRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<UsersDTO> Users_RetrieveByID(string id)
		{
			var rpt = new UsersRepository();
			var lstDto = new List<UsersDTO>();
            var lstEntity = rpt.RetrieveByID(Guid.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(UsersDTO.Translate(item));
			}
			return lstDto;
		}
        public List<UsersDTO> Users_RetrieveByUserName(string username)
        {
            var rpt = new UsersRepository();
            var lstDto = new List<UsersDTO>();
            var lstEntity = rpt.RetrieveByUserName(username);
            foreach (var item in lstEntity)
            {
                lstDto.Add(UsersDTO.Translate(item));
            }
            return lstDto;
        }
        public bool Users_ChangePassword(string userName, string oldpassword, string newpassword)
        {
            var rpt = new UsersRepository();
            return rpt.ChangePassword(userName, oldpassword, newpassword);
        }
        public string Users_ResetPassword(string userName, string email)
        {
            var rpt = new UsersRepository();
            return rpt.ResetPassword(userName, email);
        }
		/////////////////// CITY ///////////////////
		public List<CityDTO> City_SelectAll()
		{
			var lstDto = new List<CityDTO>();
			var rpt = new CityRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(CityDTO.Translate(item));
			}
			return lstDto;
		}
		public CityDTO City_SelectByID(string id)
		{
			var rpt = new CityRepository();
            return CityDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public bool City_Insert(CityDTO obj)
		{
			var rpt = new CityRepository();
			var entity = CityDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool City_Delete(string id)
		{
			var rpt = new CityRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool City_Update(CityDTO obj)
		{
			var rpt = new CityRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<CityDTO> City_RetrieveByID(string id)
		{
			var rpt = new CityRepository();
			var lstDto = new List<CityDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(CityDTO.Translate(item));
			}
			return lstDto;
		}
		/////////////////// ORDERS ///////////////////
		public List<OrdersDTO> Orders_SelectAll()
		{
			var lstDto = new List<OrdersDTO>();
			var rpt = new OrdersRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrdersDTO.Translate(item));
			}
			return lstDto;
		}
		public OrdersDTO Orders_SelectByID(string id)
		{
			var rpt = new OrdersRepository();
            return OrdersDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public int Orders_Insert(OrdersDTO obj)
		{
			var rpt = new OrdersRepository();
			var entity = OrdersDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool Orders_Delete(string id)
		{
			var rpt = new OrdersRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool Orders_Update(OrdersDTO obj)
		{
			var rpt = new OrdersRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<OrdersDTO> Orders_RetrieveByID(string id)
		{
			var rpt = new OrdersRepository();
			var lstDto = new List<OrdersDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrdersDTO.Translate(item));
			}
			return lstDto;
		}
		public List<OrdersDTO> Orders_SelectByIDUsers(string idUsers)
		{
			var rpt = new OrdersRepository();
			var lstDto = new List<OrdersDTO>();
            var lstEntity = rpt.SelectByIDUsers(Guid.Parse(idUsers));
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrdersDTO.Translate(item));
			}
			return lstDto;
		}
        public List<OrdersDTO> Orders_SelectByIDRestaurant(string idrestaurant)
        {
            var rpt = new OrdersRepository();
            var lstDto = new List<OrdersDTO>();
            var lstEntity = rpt.SelectByIDRestaurant(int.Parse(idrestaurant));
            foreach (var item in lstEntity)
            {
                lstDto.Add(OrdersDTO.Translate(item));
            }
            return lstDto;
        }
		/////////////////// DISTRICT ///////////////////
		public List<DistrictDTO> District_SelectAll()
		{
			var lstDto = new List<DistrictDTO>();
			var rpt = new DistrictRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(DistrictDTO.Translate(item));
			}
			return lstDto;
		}
		public DistrictDTO District_SelectByID(string id)
		{
			var rpt = new DistrictRepository();
            return DistrictDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public bool District_Insert(DistrictDTO obj)
		{
			var rpt = new DistrictRepository();
			var entity = DistrictDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool District_Delete(string id)
		{
			var rpt = new DistrictRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool District_Update(DistrictDTO obj)
		{
			var rpt = new DistrictRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<DistrictDTO> District_RetrieveByID(string id)
		{
			var rpt = new DistrictRepository();
			var lstDto = new List<DistrictDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(DistrictDTO.Translate(item));
			}
			return lstDto;
		}
		public List<DistrictDTO> District_SelectByIDCity(string idcity)
		{
			var rpt = new DistrictRepository();
			var lstDto = new List<DistrictDTO>();
            var lstEntity = rpt.SelectByIDCity(int.Parse(idcity));
			foreach (var item in lstEntity)
			{
				lstDto.Add(DistrictDTO.Translate(item));
			}
			return lstDto;
		}
		/////////////////// ORDERDETAIL ///////////////////
		public List<OrderDetailDTO> OrderDetail_SelectAll()
		{
			var lstDto = new List<OrderDetailDTO>();
			var rpt = new OrderDetailRepository();
			var lstEntity = rpt.SelectAll();
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrderDetailDTO.Translate(item));
			}
			return lstDto;
		}
		public OrderDetailDTO OrderDetail_SelectByID(string id)
		{
			var rpt = new OrderDetailRepository();
            return OrderDetailDTO.Translate(rpt.SelectByID(int.Parse(id)));
		}
		public bool OrderDetail_Insert(OrderDetailDTO obj)
		{
			var rpt = new OrderDetailRepository();
			var entity = OrderDetailDTO.Reverse(obj);
			return rpt.Insert(entity);
		}
		public bool OrderDetail_Delete(string id)
		{
			var rpt = new OrderDetailRepository();
            return rpt.Delete(int.Parse(id));
		}
		public bool OrderDetail_Update(OrderDetailDTO obj)
		{
			var rpt = new OrderDetailRepository();
			var entity = rpt.SelectByID(obj.ID);
			obj.UpdateEntity(ref entity);
			return rpt.Save();
		}
		public List<OrderDetailDTO> OrderDetail_RetrieveByID(string id)
		{
			var rpt = new OrderDetailRepository();
			var lstDto = new List<OrderDetailDTO>();
            var lstEntity = rpt.RetrieveByID(int.Parse(id));
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrderDetailDTO.Translate(item));
			}
			return lstDto;
		}
		public List<OrderDetailDTO> OrderDetail_SelectByIDOrder(string idorder)
		{
			var rpt = new OrderDetailRepository();
			var lstDto = new List<OrderDetailDTO>();
            var lstEntity = rpt.SelectByIDOrder(int.Parse(idorder));
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrderDetailDTO.Translate(item));
			}
			return lstDto;
		}
		public List<OrderDetailDTO> OrderDetail_SelectByIDFood(string idfood)
		{
			var rpt = new OrderDetailRepository();
			var lstDto = new List<OrderDetailDTO>();
            var lstEntity = rpt.SelectByIDFood(int.Parse(idfood));
			foreach (var item in lstEntity)
			{
				lstDto.Add(OrderDetailDTO.Translate(item));
			}
			return lstDto;
		}

        /////////////////// CART ///////////////////
        public List<CartDTO> Cart_SelectByCartID(string cartid)
        {
            var rpt = new CartRepository();
            var lstDto = new List<CartDTO>();
            var lstEntity = rpt.SelectByCartID(cartid);
            foreach (var item in lstEntity)
            {
                lstDto.Add(CartDTO.Translate(item));
            }
            return lstDto;
        }
        public CartDTO Cart_SelectByIDAndRecordID(string cartid, string recordid)
        {
            var rpt = new CartRepository();
            return CartDTO.Translate(rpt.SelectByIDAndRecordID(cartid, int.Parse(recordid)));
        }
        public CartDTO Cart_SelectByRecordID(string recordid)
        {
            var rpt = new CartRepository();
            return CartDTO.Translate(rpt.SelectByRecordID(int.Parse(recordid)));
        }
        public CartDTO Cart_SelectByIDAndFood(string cartid, string foodid)
        {
            var rpt = new CartRepository();
            return CartDTO.Translate(rpt.SelectByIDAndFood(cartid, int.Parse(foodid)));
        }
        public bool Cart_Insert(CartDTO obj)
        {
            var rpt = new CartRepository();
            var entity = CartDTO.Reverse(obj);
            return rpt.Insert(entity);
        }
        public bool Cart_Delete(string cartid, string recordid)
        {
            var rpt = new CartRepository();
            return rpt.Delete(cartid, int.Parse(recordid));
        }
        public bool Cart_Empty(string cartid)
        {
            var rpt = new CartRepository();
            return rpt.Empty(cartid);
        }
        public bool Cart_Update(CartDTO obj)
        {
            var rpt = new CartRepository();
            var entity = rpt.SelectByIDAndFood(obj.CartId, obj.FoodID);
            obj.UpdateEntity(ref entity);
            return rpt.Save();
        }

        /////////////////// ROLE ///////////////////
        public List<RoleDTO> Role_SelectAll()
        {
            var lstDto = new List<RoleDTO>();
            var rpt = new RoleRepository();
            var lstEntity = rpt.SelectAll();
            foreach (var item in lstEntity)
            {
                lstDto.Add(RoleDTO.Translate(item));
            }
            return lstDto;
        }

        public string Validation_ValidateUser(string username, string password)
        {
            var rpt = new ValidationRepository();
            return rpt.ValidateUser(username, password);
        }

        public string testsvc(string data)
        {
            return data;
        }

        public void UploadFile(string fileName, byte[] buffer, string oldname = "")
        {
            string basePath = HostingEnvironment.ApplicationPhysicalPath + @"\Content\Images\";
            string apPath = basePath + fileName;
            GlobalVariables.Logger.Info("oldname: " + oldname);
            if (oldname != "" && oldname != "noimages.png")
            {
                DeleteFile(basePath + oldname);
            }

            GlobalVariables.Logger.Info(apPath);

            try
            {
                using (FileStream fs = new FileStream(apPath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    fs.Write(buffer, 0, (int)buffer.Length);
                    fs.Close();
                    fs.Dispose();
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
        }

        public void DeleteFile(string filename)
        {
            try
            {
                FileInfo file = new FileInfo(HostingEnvironment.ApplicationPhysicalPath + @"\Content\Images\" + filename);
                if (file.Exists)
                {
                    file.Delete();
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
        }

        public byte[] DownloadFile(string fileName)
        {
            string apPath = HostingEnvironment.ApplicationPhysicalPath + @"\Content\Images\" + fileName;
            GlobalVariables.Logger.Info(apPath);
            byte[] img = null;

            try
            {
                FileStream fs = new FileStream(apPath, FileMode.OpenOrCreate, FileAccess.Read);
                GlobalVariables.Logger.Info("fs.Length: " + fs.Length);
                img = new byte[fs.Length];
                fs.Read(img, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                fs.Dispose();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return img;
        }

        public string TestService()
        {
            return "Success";
        }
	}
}
